package pkg.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;


public class PagedList <T> implements PaginatedList, Serializable {
    private List <T> fullList;
    
    
    private List <T> subList;
    private int objectsPerPage;
    private int pageNumber;
    private int fullListSize;
    private String sortCriterion;
    private SortOrderEnum sortOrder;
    private String searchId;
    
    
    public PagedList() {
        fullList = new ArrayList<T>();
        subList = new ArrayList<T>();
        objectsPerPage = 0;
        pageNumber = 1;
        fullListSize = 0;
        sortCriterion = null;
        sortOrder = SortOrderEnum.ASCENDING;
        searchId = null;
    }
    
    
    public void setResults(List <T>results) {
        this.fullList = results;
        this.setFullListSize(results.size());
        this.setObjectsPerPage(50);
    }
    
    public void setObjectsPerPage(int objectsPerPage) {
        this.objectsPerPage = objectsPerPage;
    }
    
    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }
    
    public void setFullListSize(int fullListSize) {
        this.fullListSize = fullListSize;
    }
    
    public void setSortCriterion(String sortCriterion) {
        this.sortCriterion = sortCriterion;
    }
    
    public void setSortOrder(SortOrderEnum sortOrder) {
        this.sortOrder = sortOrder;
    }
    
    public void setSearchId(String searchId) {
        this.searchId = searchId;
    }
    
    
    public List<T> getSubList() {
        return subList;
    }
    
    public void setSubList(List<T> subList) {
        this.subList = subList;
    }
    
    public List <T> getListKeys() {
        int begin = (getPageNumber()-1) * getObjectsPerPage();
        int end = begin + getObjectsPerPage();
        return fullList.subList(begin, end < getFullListSize() ? end : getFullListSize());
    }
    
    
    public List getList() {
        int begin = (getPageNumber()-1) * getObjectsPerPage();
        int end = begin + getObjectsPerPage();
        return fullList.subList(begin, end < getFullListSize() ? end : getFullListSize());
    }
    
    public int getPageNumber() {
        return pageNumber;
    }
    
    public int getObjectsPerPage() {
        return objectsPerPage;
    }
    
    public int getFullListSize() {
        return fullListSize;
    }
    
    public String getSortCriterion() {
        return sortCriterion;
    }
    
    public SortOrderEnum getSortDirection() {
        return sortOrder;
    }
    
    public String getSearchId() {
        return searchId;
    }
}
